﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using SAAS.ServiceInterface;
using System.IO;

namespace SAAS.IISHost
{
    public class FileManager : IFileManager
    {
        public List<FileData> GetFilesList()
        {
            string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"];
            List<FileData> fileList = new List<FileData>();
            if (System.IO.Directory.Exists(filePath))
            {
                string[] files = Directory.GetFiles(filePath);
                foreach (string s in files)
                {
                    var file = new FileInfo(s);
                    fileList.Add(new FileData()
                    {
                        Name = file.Name,
                        Length = file.Length,
                        CreateTime = file.CreationTime
                    });
                }
            }

            return fileList;

        }

        public FileData GetFiles(string fileName)
        {
            string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"] + "/" + fileName;
            if (File.Exists(filePath))
            {
                FileData file = new FileData();
                using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                    file.Name = fileName;
                    file.Offset = fs.Length;
                    fs.Close();
                }
                return file;
            }
            return null;
        }

        public FileData UplodaFile(FileData file)
        {
            string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"] + "/" + file.Name;
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                long offset = file.Offset;
                BinaryWriter writer = new BinaryWriter(fs);
                writer.Seek((int)offset, SeekOrigin.Begin);
                writer.Write(file.Data);

                file.Offset = fs.Length;
                file.Data = null;
                writer.Close();
                fs.Close();
            }

            return file;
        }


        public FileData DownlownFile(FileData file)
        {
            string filePath = file.Name;
            using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                file.Length = stream.Length;
                if (file.Offset < file.Length)
                {
                    int maxLength = Constants.MaxLength;
                    int count = (int)(file.Length - file.Offset > maxLength ? maxLength : file.Length - file.Offset);
                    file.Data = new byte[count];
                    stream.Position = file.Offset;
                    stream.Read(file.Data, 0, file.Data.Length);
                    //file.Offset = stream.Length;
                }
                stream.Close();
            }
            return file;
        }
    }
}
